package com.code.leetcode.before50;

/**
 * @author codeDorado
 * @version 1.0
 * @date 2020/7/21 11:23
 */
public class The34 {

    public int[] searchRange(int[] nums, int target) {
        int[] rtns = new int[]{-1, -1};
        if (nums == null || nums.length < 1) return rtns;
        int left = 0, right = nums.length - 1;
        int mid = 0;
        while (left <= right) {
            mid = (left + right) / 2;
            if (nums[mid] == target) break;
            if (nums[mid] < target) left = mid + 1;
            if (nums[mid] > target) right = mid - 1;
        }
        if (left > right) return rtns;
        left = mid;
        right = mid;
        while (left > 0 && nums[left] == target) {
            left--;
        }
        while (right < nums.length - 1 && nums[right] == target) {
            right ++;
        }

        left = nums[left] == target ? left : left + 1;
        right = nums[right] == target ? right : right - 1;
        return new int[]{left, right};
    }

}
